<?php

/*
info: 商区分类管理
date: 2011-11-23
*/

if (!defined('WEBINROOT'))
{
	header("HTTP/1.0 404 Not Found");
	exit();
}

class NewsCategoryDataModel extends CategoryDataModel {
	protected $categoryItemClass='NewsCategoryItem';
	protected $cachekey = 'category_news';

	protected $tableName = 'news_category';
	protected $title = '资讯';

	protected $supportCheckRelation = true;
	protected $relationTableName = 'news'; //关联表
	protected $relationField = 'news_category_id'; //关联字段
	protected $relationErrorMessage = '该分类下还有资讯信息, 不能删除.'; //关联表错误提示信息

	public function getCategoryOptionByChannel($channel = 0, $typeid = 0) {
		$tempResult = array();
		$allClass = $this->getChildForLayer($typeid);
		foreach ($allClass as $categoryID => $category) {
			if ($category['channeltype'] == $channel || $this->checkChildByChannel($categoryID, $channel)) {
				$category['showName'] = str_repeat("─", $category['layer'] - 1).$category['name'];
				$category['tdclass'] = $category['layer'] == 1 ? ' class="evenrow"' : '';
				if ($category['ispart']) {
					$category['showName'] .= "(封面频道)";
					$category['class'] = 'option1';
				} elseif ($category['channeltype'] != $channel) {
					$category['class'] = 'option1';
				} else {
					$category['class'] = "option3";
				}

				$tempResult[$categoryID] = $category;
			}
		}
		return $tempResult;
	}

	private function checkChildByChannel($categoryID = 0, $channel = 0) {
		$tempReturn = false;
		if ($childClass = $this->getChildForLayer($categoryID, 0, false)) {
			foreach ($childClass as $categoryID => $category) {
				if ($category['channeltype'] == $channel) {
					$tempReturn = true;
					break;
				}
			}
		}

		return $tempReturn;
	}

	public function getChildIds($categoryID = 0, $layer = 0, $containSelf = true) {
		$tempReturn = array();
		if ($childClass = $this->getChildForLayer($categoryID, $layer, $containSelf)) {
			foreach ($childClass as $child) {
				$tempReturn[] = $child['id'];
			}
		}

		return $tempReturn;
	}

	public function checkChannel($typeid = 0, $channel = 0) {
		if ($type = $this->getItem($typeid)) {
			if ($type['ispart']) {
				return false;
			} elseif ($type['channeltype'] != $channel) {
				return false;
			}

			return true;
		}

		return false;
	}

}

class NewsCategoryItem extends CategoryItem {
	protected $attribs;

	public function __construct($args) {
		parent::__construct($args);

		$this->setAttribs('ispart', $args['ispart']);
		$this->setAttribs('channeltype', $args['channeltype']);
		$this->setAttribs('seotitle', $args['seotitle']);
		$this->setAttribs('keywords', $args['keywords']);
		$this->setAttribs('description', $args['description']);
	}

	protected function setAttribs($field, $value) {
		$this->attribs[$field] = $value;
	}

	protected function getAttribs($field) {
		return isset($this->attribs[$field]) ? $this->attribs[$field] : false;
	}

	public function toArray() {
		$result = parent::toArray();

		$result['ispart'] = $this->getAttribs('ispart');
		$result['channeltype'] = $this->getAttribs('channeltype');
		$result['seotitle'] = $this->getAttribs('seotitle');
		$result['keywords'] = $this->getAttribs('keywords');
		$result['description'] = $this->getAttribs('description');

		return $result;
	}
}